﻿using LD.Vision;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ToolCreateShapeFrm : FrmToolBase
    {
        public ToolCreateShapeFrm()
        {
            InitializeComponent();

            this.Text = TB_Tool.Name;
            this.btn_ok.Click += Btn_ok_Click;
            this.Shown += ToolReadImageFrm_Shown;
        }

        private void ToolReadImageFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            int type = 0;

            if (this.rab_grayModel.Checked)
                type = 1;

            var param = Newtonsoft.Json.JsonConvert.SerializeObject(new CreateShapeParam()
            {
                Camera = this.cob_camera.Text,
                IsZoom = this.chk_zoom.Checked,
                ModelType = type,
                Name = this.txt_model.Text,
                Roi = this.cob_roi.SelectedIndex
            });
            TB_Tool.Param = param;
        }

        private void Init()
        {
            foreach (var item in CameraFactory.Instance.CameraList)
            {
                this.cob_camera.Items.Add(item.Key);
            }

            if (TB_Tool.Param == null)
                return;
            var param = Newtonsoft.Json.JsonConvert.DeserializeObject<CreateShapeParam>(TB_Tool.Param);
            if (param != null)
            {
                this.cob_camera.Text = param.Camera;
                this.chk_zoom.Checked = param.IsZoom;
                this.chk_zoom.Checked = param.IsZoom;
                this.txt_model.Text = param.Name;
                this.cob_roi.SelectedIndex = param.Roi;
                int type = param.ModelType;
                if (type == 0)
                    this.rab_shapeModel.Checked = true;
                if (type == 1)
                    this.rab_grayModel.Checked = true;
            }
        }

        private void cob_camera_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cob_camera.SelectedIndex > -1)
            {
                LoadRoi(cob_camera.Text);
            }
        }
        private void LoadRoi(string camera)
        {
            this.cob_roi.Items.Clear();
            foreach (var roi in CameraFactory.Instance.CameraList[camera].Roi)
            {
                this.cob_roi.Items.Add(roi.getRegion().GetType());
            }
        }

        private void cob_roi_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
